﻿#include "frmmain.h"
#include "ui_frmmain.h"
#include "quiwidget.h"
#include <qfile.h>

frmMain::frmMain(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::frmMain)
{
    ui->setupUi(this);

    this->initForm();
//    QUIWidget::setFormInCenter(this);

}

frmMain::~frmMain()
{
    delete ui;
}

void frmMain::setStyle(const QString &str)
{
    static QString qss;

    if (qss == str) {
        return;
    }
    qss = str;
    QString paletteColor = str.mid(20, 7);
    qApp->setPalette(QPalette(QColor(paletteColor)));
    qApp->setStyleSheet(str);

}

void frmMain::initForm()
{

    ui->widgetTop->setProperty("nav", "top");
//    ui->labTitle->setText("后台服务管理平台");
//    ui->labTitle->setFont(QFont("Microsoft Yahei", 20));

    QSize icoSize(32, 32);
    int icoWidth = 85;

    //设置顶部导航按钮
    QList<QToolButton *> tbtns = ui->widgetTop->findChildren<QToolButton *>();
    foreach (QToolButton *btn, tbtns) {
        btn->setIconSize(icoSize);
        btn->setMinimumWidth(icoWidth);
        btn->setCheckable(true);
        connect(btn, SIGNAL(clicked()), this, SLOT(buttonClick()));
    }

    //不同窗口布局
    this->m_frmmanager = new FrmManager();
    ui->stackedWidget->addWidget(this->m_frmmanager);
    this->m_frmcontrol = new FrmControl();
    ui->stackedWidget->addWidget(this->m_frmcontrol);
    this->m_frmsetup = new FrmSetup();
    ui->stackedWidget->addWidget(this->m_frmsetup);
    this->m_frmabout = new FrmAbout();
    ui->stackedWidget->addWidget(this->m_frmabout);

}

void frmMain::buttonClick()
{
    QToolButton *b = (QToolButton *)sender();
    QString name = b->text();

    QList<QToolButton *> tbtns = ui->widgetTop->findChildren<QToolButton *>();
    foreach (QToolButton *btn, tbtns) {
        if (btn == b) {
            btn->setChecked(true);
        } else {
            btn->setChecked(false);
        }
    }

    if (name == "主界面") {
       ui->stackedWidget->setCurrentIndex(0);
    } else if (name == "系统管理") {
       ui->stackedWidget->setCurrentIndex(1);
    } else if (name == "系统设置") {
       ui->stackedWidget->setCurrentIndex(2);
    } else if (name == "帮助样例") {
       ui->stackedWidget->setCurrentIndex(3);
    } else if (name == "系统退出") {
        exit(0);
    }

}


void frmMain::changeStyle(const QString &qssFile)
{
    QString fileName = qssFile;

    if (!fileName.isEmpty()) {
        QFile file(fileName);

        if (file.open(QFile::ReadOnly)) {
            QString str = file.readAll();
            currentFile = fileName;
            currentQSS = str;
            setStyle(str);
        }
    }
}

